package struts;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import pojo.Difficulty;
import service.ClassService;
import service.QuestionGenerator;
import base.JsonPacker;

import com.opensymphony.xwork2.ActionSupport;

import dao.MyclassDAO;

public class ClassAction extends ActionSupport{
	private ClassService classService;

	public void getClassList(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String teacherUserName = request.getParameter("teacherUserName");
		try {
			write(ServletActionContext.getResponse(), 
					new JsonPacker().toJsonArray(classService.getClassList(teacherUserName)));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void getClassInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int classId = Integer.parseInt(request.getParameter("classId"));
		try {
			write(ServletActionContext.getResponse(), 
					new JsonPacker().toJson(classService.getClassInfo(classId)));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void write(HttpServletResponse response,String result){
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public ClassService getClassService() {
		return classService;
	}

	public void setClassService(ClassService classService) {
		this.classService = classService;
	}

}
